// https://www.npmjs.com/package/mysql
const mysql = require('mysql');

const pool = mysql.createPool({
  host: '127.0.0.1',
  port: 3306,
  database: 'test',
  user: 'root',
  password: '',
});

function exec(sql, callback) {
  pool.getConnection(function (err, conn) {
    if (err) {
      if (callback) {
        callback(err, null, null);
      }
      return;
    }

    conn.query(sql, function (err, result, fields) {
      if (err) {
        if (callback) {
          callback(err, null, null);
        }
        return;
      }

      if (callback) {
        callback(null, result, fields);
      }
    });
  });
}

module.exports = exec;
